<?php
/**
 * Created by PhpStorm.
 * User: FDG296
 * Date: 2017/8/2
 * Time: 16:42
 */
namespace common\models;

use Yii;
use yii\db\ActiveRecord;

class CustFavorite extends ActiveRecord{

    public static function tableName(){
        return '{{%cust_favorite}}';
    }

    /**
     * 删除心愿单
     * @param $goods_id int 商品goods_id
     * @param $member_id int  会员id
     * @return int
     */
    public function delCollection($main_goods_id,$member_id){
        $where = ['main_goods_id'=>$main_goods_id,'cust_id'=>$member_id];
		$result = $this::deleteAll($where);
        return $result;
    }

	/**
	 * @param add by FDG367 2018-03-08 542 【商城需求】心愿单界面增加预约到店功能，点击预约到店，新增页面填写用户信息 start
	 * @param 获取当前用户的收藏信息
	 * @param 当前登录用户$member_id
	 * @param 是否要返回有效的商品$effective
	 */
	public static function getCollection($member_id,$effective=true){
		$where = [
			self::tableName().'.cust_id'=>$member_id,
		];

		if($effective){
			$where =array_merge($where,[GoodsList::tableName().'.use_flg'=>GoodsList::USE_FLG_YES]);
		}

		$collectionInfo		= self::find()
				->select([
					GoodsList::tableName().'.main_goods_id',
					self::tableName().'.goods_id',
					self::tableName().'.create_time',
					GoodsList::tableName().'.sku',
					GoodsList::tableName().'.use_flg',
					GoodsList::tableName().'.goods_main_pic',
					GoodsList::tableName().'.brand_id',
					GoodsList::tableName().'.goods_name_cn',
					GoodsList::tableName().'.series',
					GoodsList::tableName().'.model',
					GoodsList::tableName().'.brand_name',
					GoodsBrand::tableName().'.country_icon',
					GoodsList::tableName().'.goods_pic_title',
					GoodsList::tableName().'.goods_pic_alt',
					GoodsList::tableName().'.hidden_price',
					GoodsList::tableName().'.market_price',
					GoodsList::tableName().'.unit',
					GoodsList::tableName().'.sale_price',
				])
				->where($where)
			->andFilterWhere(['>',GoodsList::tableName().'.main_goods_id',0])
			->joinWith('goodsListRelation')
				//->groupBy([self::tableName().'.goods_id'])
				//->createCommand()->getRawSql();
					->orderBy([self::tableName().'.create_time'=>SORT_DESC])
				->asArray()
				->all();

		if(!$collectionInfo){
			return [];
		}

		return $collectionInfo;
	}

	/**
	 * @param add by FDG367 2018-03-08 542 【商城需求】心愿单界面增加预约到店功能，点击预约到店，新增页面填写用户信息 start 关联表j_goods_sale
	 * @return \yii\db\ActiveQuery
	 */
	public function getGoodsRelation()
	{
		// hasOne要求返回两个参数 第一个参数是关联表的类名 第二个参数是两张表的关联关系
		return $this->hasOne(GoodsSale::className(), ['goods_id' => 'goods_id'])->joinWith('brandInfo');
	}
	/**
	 * @param add by FDG367 2018-03-08 542 【商城需求】心愿单界面增加预约到店功能，点击预约到店，新增页面填写用户信息 start 关联表j_goods_sale
	 * @return \yii\db\ActiveQuery
	 */
	public function getGoodsListRelation()
	{
		// hasOne要求返回两个参数 第一个参数是关联表的类名 第二个参数是两张表的关联关系
		return $this->hasOne(GoodsList::className(), ['main_goods_id' => 'main_goods_id'])->joinWith('brandInfo')
			->select([
					GoodsList::tableName().'.main_goods_id',
					GoodsList::tableName().'.brand_id',

				]
			);
	}
	/**
	 * @param add by FDG367 2018-03-08 542 【商城需求】心愿单界面增加预约到店功能，点击预约到店，新增页面填写用户信息 start 添加心愿单
	 * @return object
	 */
	public function insertInfo($data_now){

		$data_now['session_id'] = session_id();
		$data_now['create_time'] = $data_now['create_time']? $data_now['create_time']:date('Y-m-d H:i:s',time());

		//插入对应的信息
		$this->load($data_now,'');
		return $this->save();
	}

	/**
	 * @param add by FDG367 2018-03-08 542 【商城需求】心愿单界面增加预约到店功能，点击预约到店，新增页面填写用户信息 start 添加心愿单
	 * @return array
	 */
	public function rules()
	{
		return [
			[['session_id','cust_id','cust_mobile','goods_id','sku','main_goods_id','create_time'], 'safe'],

		];
	}

}